package com.dreamers.extend;

import com.dreamers.core.Collider2D;
import com.dreamers.core.Global;
import com.dreamers.core.MonoBehaviour;
import com.dreamers.map.MapArray;
import com.dreamers.pathcore.Vector2;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class AStarWorld extends MonoBehaviour {


    static Collider2D[][] collider2DS = new Collider2D[MapArray.map.length][MapArray.map[0].length];

    @Override
    public void start() {
        System.out.println(MapArray.map.length);
        System.out.println(MapArray.map[0].length);
    }

    @Override
    public void render(Graphics g) {

        for (int y = 0; y < MapArray.map.length; y++) {
            for (int x = 0; x < MapArray.map[0].length; x++) {
                collider2DS[y][x] = new Collider2D();
                if (MapArray.map[y][x] == 1) {
                    drawMapObject(g, y, x, Color.GREEN, "image/地图/冰水深明星.png");
                } else if (MapArray.map[y][x] == 2) {
                    drawMapObject(g, y, x, Color.red, "image/地图/金属圆角.png");
                } else if (MapArray.map[y][x] == 3) {
                    drawMapObject(g, y, x, Color.orange, "image/地图/金属圆角.png");
                }else if (MapArray.map[y][x] == 5) {
                    drawMapObject(g, y, x, Color.BLUE, "image/地图/金属圆角.png");
                }
            }
        }

    }

    private void drawMapObject(Graphics g, int y, int x, Color color, String s) {
        g.setColor(color);
        g.fill3DRect(Camera.offsetX + x * Global.CELL_SIZE,
                Camera.offsetY + y * Global.CELL_SIZE,
                Global.CELL_SIZE, Global.CELL_SIZE, true);
/*        Image image = Toolkit.getDefaultToolkit().getImage(s);
        g.drawImage(image, Camera.offsetX + x * Global.CELL_SIZE,
                Camera.offsetY + y * Global.CELL_SIZE,
                Global.CELL_SIZE, Global.CELL_SIZE, null);*/

        collider2DS[y][x].setRectangle(new Rectangle(Camera.offsetX + x * Global.CELL_SIZE,
                Camera.offsetY + y * Global.CELL_SIZE,
                Global.CELL_SIZE, Global.CELL_SIZE));
        collider2DS[y][x].setIndex(new Vector2(x, y));
    }



    public static List<Vector2> getNeighour(Vector2 index) {

        List<Vector2> vector2List = new ArrayList<>();
        if (index.getX() - 1 >= 0) {
            vector2List.add(new Vector2(index.getX() - 1, index.getY()));
        }

        if (index.getX() + 1 < MapArray.map[0].length) {
            vector2List.add(new Vector2(index.getX() + 1, index.getY()));
        }

        if (index.getY() - 1 >= 0) {
            vector2List.add(new Vector2(index.getX(), index.getY() - 1));
        }

        if (index.getY() + 1 < MapArray.map.length) {
            vector2List.add(new Vector2(index.getX(), index.getY() + 1));
        }
        return vector2List;
    }


}
